import { getToken, removeToken, setToken } from '@/utils/auth'
import { getInfo, getUserDetailById } from '@/api/user'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    // 更新token
    updateToken(state, token) {
      state.token = token
      setToken(token)
    },
    delToken(state, token) {
      state.token = ''
      removeToken()
    },
    // 设置用户信息
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    // 删除用户信息
    reomveUserInfo(state) {
      state.userInfo = {}
    }

  },
  actions: {
    async getUserInfo(context) {
      const res = await getInfo()
      console.log('获取用户信息', res)
      // 根据id 获取用户详情
      const info = await getUserDetailById(res.data.userId)
      console.log('根据id获取详细信息', info)
      context.commit('setUserInfo', { ...info.data, ...res.data })
    }
  }
}
